import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from time import sleep

driver=webdriver.Firefox()
driver.get('http://10.59.9.14:7003/')
# 点击登录
driver.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/ul/li[1]/a').click()
time.sleep(2)
print('---------------------------------------------------------')
print('有运费的搜索商品并下单的业务')
print('---------------------------------------------------------')
# 预期1 跳转到登录页面
dl=driver.find_element(By.CLASS_NAME,'reg-new').text
print('1 会员登录：',dl)
# 输入用户名和密码
driver.find_element(By.NAME,'username').send_keys('13811033962')
driver.find_element(By.NAME,'password').send_keys('123456')
time.sleep(5)
#点击登录按钮
driver.find_element(By.XPATH,'/html/body/div[4]/div/div[1]/div[2]/form/div[5]/a').click()
time.sleep(5)
# 预期2 登陆成功后跳转到会员中心
hy=driver.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/ul/li[1]/a').text
print('2 跳转会员中心：',hy)
# 预期3 用户余额
ye=driver.find_element(By.CLASS_NAME,'color-black').text
ye1=ye.replace('¥ ','')
print('3 账户余额：',ye1)
#点击跳转到首页
driver.find_element(By.LINK_TEXT,'首页').click()
time.sleep(5)
# 搜索商品并跳转
srk=driver.find_element(By.CLASS_NAME,'search-input')
srk.send_keys('长崎一号')
srk.send_keys(Keys.ENTER)
time.sleep(5)
#点击跳转到商品详情页
driver.find_element(By.XPATH,'//img[@class="lazy" and @src="/uploads/images/20250721/f2016cd8a6bf8dfe54b60234cf18fe91.png"]').click()
time.sleep(2)
# 预期4 商品价格
sp=driver.find_element(By.ID,'sell-price-box').text
print('4 商品价格:',sp)
time.sleep(2)
# 修改商品数量
num=driver.find_element(By.ID,'store_nums')
num.clear()
num.send_keys(2)
time.sleep(2)
# 确认订单
driver.find_element(By.CLASS_NAME,'shop-btn').click()
time.sleep(2)
#预期5 成功跳转到订单页面
qr=driver.find_element(By.XPATH,'/html/body/div[4]/div[1]/a[2]').text
print('5 确认订单：',qr)
# 选择收货地址
driver.find_element(By.ID,'address-31').click()
time.sleep(2)
# 预期6 所花的运费
yf=driver.find_element(By.ID,'real-freight').text
yf1=yf.replace('+','')
print('6 运费:',yf1)
# 预期7 商品应付的总金额
zje=driver.find_element(By.XPATH,'//*[@id="order-amount"]').text
print('7 应付总金额：',zje)
assert float(zje) == float(sp) * 2 + float(yf1)
# 跳转到订单完成页
driver.find_element(By.ID,'order-button').click()
time.sleep(2)
# 预期8 跳转订单页面反应订单完成
dd=driver.find_element(By.XPATH,'/html/body/div[4]/div[1]/a[2]').text
print('8 跳转订单页面反应订单完成:',dd)
# 预期9 跳转订单页面反应订单编号
ddbh=driver.find_element(By.XPATH,'/html/body/div[4]/div[4]/div[2]/ul/li[1]').text
ddbh1=ddbh.replace('订单编号：','')
print('9 订单编号:',ddbh1)
# 预期10 跳转订单页面反应订单总金额
ddje=driver.find_element(By.XPATH,'/html/body/div[4]/div[4]/div[2]/ul/li[2]').text
ddje1=ddje.replace('订单金额：￥','')
print('10 订单应付总金额:',ddje1)
assert ddje1 == zje
# 跳转到我的订单页面
driver.find_element(By.XPATH,'/html/body/div[4]/div[3]/p/a').click()
time.sleep(2)
# 预期11 跳转订单页面反应订单号
ddh=driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div/div[2]/table[2]/tbody/tr[2]/td[2]').text
print('11 订单号:',ddh)
#跳转到会员中心
driver.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/ul/li[3]/a').click()
time.sleep(2)
# 预期12 跳转会员中心页面反应购买商品后的账户余额
syye=driver.find_element(By.CLASS_NAME,'color-black').text
syye1=syye.replace('¥ ','')
print('12 购买商品后的账户余额：',syye1)

print('---------------------------------------------------------')
print('无运费并搜索下单的商品业务')
print('---------------------------------------------------------')
#点击跳转到首页
driver.find_element(By.LINK_TEXT,'首页').click()
time.sleep(5)
# 搜索商品并跳转
srk=driver.find_element(By.CLASS_NAME,'search-input')
srk.send_keys('小男孩')
srk.send_keys(Keys.ENTER)
time.sleep(5)
#点击跳转到商品详情页
driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div[2]/div[1]/ul/li/div[1]/span/a/img').click()
time.sleep(2)
# 预期1 商品价格
s_p=driver.find_element(By.ID,'sell-price-box').text
print('1 商品价格:',s_p)
time.sleep(2)
# 修改商品数量
num=driver.find_element(By.ID,'store_nums')
num.clear()
num.send_keys(2)
time.sleep(2)
# 确认订单
driver.find_element(By.CLASS_NAME,'shop-btn').click()
time.sleep(2)
#预期2 成功跳转到订单页面
q_r=driver.find_element(By.XPATH,'/html/body/div[4]/div[1]/a[2]').text
print('2 确认订单：',q_r)
# 选择收货地址
driver.find_element(By.ID,'address-31').click()
time.sleep(2)
# 预期3 所花的运费
y_f=driver.find_element(By.ID,'real-freight').text
y_f1=y_f.replace('+','')
print('3 运费:',y_f1)
# 预期4 商品应付的总金额
z_je=driver.find_element(By.XPATH,'//*[@id="order-amount"]').text
print('4 应付总金额：',z_je)
assert float(z_je) == float(s_p) * 2 + float(y_f1)
# 跳转到订单完成页
driver.find_element(By.ID,'order-button').click()
time.sleep(2)
# 预期5 跳转订单页面反应订单完成
d_d=driver.find_element(By.XPATH,'/html/body/div[4]/div[1]/a[2]').text
print('5 跳转订单页面反应订单完成:',d_d)
# 预期6 跳转订单页面反应订单编号
d_dbh=driver.find_element(By.XPATH,'/html/body/div[4]/div[4]/div[2]/ul/li[1]').text
d_dbh1=d_dbh.replace('订单编号：','')
print('6 订单编号:',d_dbh1)
# 预期7 跳转订单页面反应订单总金额
d_dje=driver.find_element(By.XPATH,'/html/body/div[4]/div[4]/div[2]/ul/li[2]').text
d_dje1=d_dje.replace('订单金额：￥','')
d_dje.split()
print('7 订单应付总金额:',d_dje1)
assert d_dje1 == z_je
# 跳转到我的订单页面
driver.find_element(By.XPATH,'/html/body/div[4]/div[3]/p/a').click()
time.sleep(2)
# 预期8 跳转订单页面反应订单号
d_dh=driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div/div[2]/table[2]/tbody/tr[2]/td[2]').text
print('8 订单号:',d_dh)
#跳转到会员中心
driver.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/ul/li[3]/a').click()
time.sleep(2)

# 预期9 跳转会员中心页面反应购买商品后的账户余额
s_yye=driver.find_element(By.CLASS_NAME,'color-black').text
s_yye1=s_yye.replace('¥ ','')
print('9 购买商品后的账户余额：',s_yye1)
# 关闭页面
driver.quit()




